package com.doge.calcalbum.business.home

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.viewpager2.widget.ViewPager2
import com.doge.calcalbum.R
import com.doge.calcalbum.analysis.*
import com.doge.calcalbum.databinding.LayoutNavigationBinding
import com.doge.calcalbum.utils.appChannel
import com.doge.calcalbum.utils.disableSkin
import com.doge.calcalbum.utils.setOnClickCallback

class NavigationLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    private val binding = LayoutNavigationBinding.inflate(LayoutInflater.from(context), this)
    private val animScaleKick = AnimationUtils.loadAnimation(context, R.anim.anims_scale_kick)!!
    private val itemViews by lazy {
        arrayOf(
            binding.tabPicture,
            binding.tabVideo,
            binding.tabSkin,
            binding.tabProfile
        )
    }
    private val itemIconViews by lazy {
        arrayOf(
            binding.tabPictureIcon,
            binding.tabVideoIcon,
            binding.tabSkinIcon,
            binding.tabProfileIcon
        )
    }
    private val itemTextViews by lazy {
        arrayOf(
            binding.tabPictureText,
            binding.tabVideoText,
            binding.tabSkinText,
            binding.tabProfileText
        )
    }
    private val skinViews by lazy {
        arrayOf(
            binding.tabSkin,
            binding.tabSkinIcon,
            binding.tabSkinText
        )
    }

    private var currentIndex = -1
    private var pager: ViewPager2? = null
    private var callback: Callback? = null

    interface Callback {
        fun onItemClick(position: Int): Boolean
        fun onItemClickAgain(position: Int)
    }

    init {
        itemViews.forEachIndexed { index, view ->
            view.setOnClickCallback {
                select(index)
                analysisTab(index)
            }
        }

        skinViews.forEach { it.isVisible = !disableSkin }
    }

    private fun analysisTab(idx: Int) {
        analysisEvent(when (idx) {
            0 -> tab_picture
            1 -> tab_video
            2 -> tab_skin
            else -> tab_profile
        })
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        pager?.currentItem?.let(::select)
    }

    private fun select(target: Int) {
        if (currentIndex == target) {
            val animId = when (currentIndex) {
                0 -> R.mipmap.tab_photoed
                1 -> R.mipmap.tab_videoed
                2 -> R.mipmap.tab_skined
                else -> R.mipmap.tab_med
            }
            select(itemIconViews[target], animId, itemTextViews[target])
            callback?.onItemClickAgain(target)
            return
        }
        if (callback?.onItemClick(target) != true) {
            currentIndex = target
            itemViews.forEachIndexed { index, _ ->
                if (target == index) {
                    val animId = when (index) {
                        0 -> R.mipmap.tab_photoed
                        1 -> R.mipmap.tab_videoed
                        2 -> R.mipmap.tab_skined
                        else -> R.mipmap.tab_med
                    }
                    select(itemIconViews[index], animId, itemTextViews[index])
                } else {
                    val restId = when (index) {
                        0 -> R.mipmap.tab_photo
                        1 -> R.mipmap.tab_video
                        2 -> R.mipmap.tab_skin
                        else -> R.mipmap.tab_me
                    }
                    reset(itemIconViews[index], restId, itemTextViews[index])
                }
            }
        }
    }

    fun attach(viewPager: ViewPager2) {
        pager = viewPager
        viewPager.currentItem.let(::select)
    }

    fun setListener(listener: Callback?) {
        callback = listener
    }

    private fun select(imageView: ImageView, animationId: Int, textView: TextView) {
        imageView.setImageResource(animationId)
        imageView.startAnimation(animScaleKick)
        textView.setTextColor(0xFF5459A7.toInt())
        textView.typeface = Typeface.DEFAULT_BOLD
    }

    private fun reset(imageView: ImageView, resId: Int, textView: TextView) {
        imageView.setImageResource(resId)
        imageView.clearAnimation()
        textView.setTextColor(0xFFCECECE.toInt())
        textView.typeface = Typeface.DEFAULT
    }
}